package com.example.iorder.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.iorder.config.Config;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class HTTPClient 
{
	private Context mContext;

	/*
	 *
	 *
	 */
	public HTTPClient(Context pContext) {
		mContext = pContext;
	}

	

	
	
	
	/*
	 *
	 *
	 */
	public String sendGETRequest(String pHost, String pPath) 
	{
		InputStream content = null;
		String lRetVal = "";
		String lURL = "http://" + pHost + pPath;

		if (!isNetworkUp()) 
		{
			lRetVal = "Error: No network";
		} 
		else 
		{
			try 
			{
				HttpClient lClient = new DefaultHttpClient();
				HttpGet lGetReq = new HttpGet(lURL);
				HttpResponse lResponse = lClient.execute(lGetReq);

				content = lResponse.getEntity().getContent();
				lRetVal = getStringFromInputStream(content);
			} 
			catch (Exception lEx) 
			{
				lRetVal = "Error(0):" + lEx.getMessage();
			}
		}

		return lRetVal;
	}

	
	
	/*
	 * 
	 * 
	 */
	public String sendPUTRequest(String pHost, String pPath, String pPUTDataString) 
	{
		InputStream content = null;
		String lRetVal = "";
		String lURL = "http://" + pHost + pPath;
		
		if (!isNetworkUp()) 
		{
			lRetVal = "Error: No network";
		} 
		else 
		{
			try
			{
				HttpClient lClient = new DefaultHttpClient();
				HttpPut lPutReq = new HttpPut(lURL);
				lPutReq.setHeader("Accept", "application/json");
				lPutReq.setHeader("Content-type", "application/json");
				lPutReq.setEntity(new StringEntity(pPUTDataString));
				HttpResponse lResponse = lClient.execute(lPutReq);
				content = lResponse.getEntity().getContent();
				lRetVal = getStringFromInputStream(content);				
			} 
			catch (Exception lEx) 
			{
				lRetVal = "Error(0):" + lEx.getMessage();
			}		
		}
		
		return(lRetVal);
	}	
	
	
	
	
	/*
	 * 
	 * 
	 */
	public String sendPOSTRequest(String pHost, List<NameValuePair> values)
	{
		InputStream content = null;
		String lRetVal = "";
		String lURL = "http://" + pHost;

		if (!isNetworkUp()) 
		{
			lRetVal = "Error: No network";
		} 
		else 
		{
			try 
			{
				HttpClient lClient = new DefaultHttpClient();
				HttpPost lPostReq = new HttpPost(lURL);
				lPostReq.setEntity(new UrlEncodedFormEntity(values));
				lPostReq.setHeader("Accept", "application/json");
				lPostReq.setHeader("Content-type", "application/json");
				HttpResponse lResponse = lClient.execute(lPostReq);
				content = lResponse.getEntity().getContent();
				lRetVal = getStringFromInputStream(content);
				
			} 
			catch (Exception lEx) 
			{
				lRetVal = "Error(0):" + lEx.getMessage();
			}
		}

		return lRetVal;
		
	}
	
	/*
	 * 
	 * 
	 */
	public String sendPOSTRequest(String pHost, String pPath, String pPostDataString)
	{
		InputStream content = null;
		String lRetVal = "";
		String lURL = "http://" + pHost + pPath;

		if (!isNetworkUp()) 
		{
			lRetVal = "Error: No network";
		} 
		else 
		{
			try 
			{
				HttpClient lClient = new DefaultHttpClient();
				HttpPost lPostReq = new HttpPost(lURL);
				StringEntity se = new StringEntity(pPostDataString);
				lPostReq.setEntity(se);
				
				lPostReq.setHeader("Accept", "application/json");
				lPostReq.setHeader("Content-type", "application/json");
				HttpResponse lResponse = lClient.execute(lPostReq);
				content = lResponse.getEntity().getContent();
				lRetVal = getStringFromInputStream(content);
			} 
			catch (Exception lEx) 
			{
				lRetVal = "Error(0):" + lEx.getMessage();
			}
		}

		return lRetVal;
		
	}
	
	/*
	 * 
	 * 
	 */
	public String sendDELETEAllOrderedMenuItemsRequest()
	{
		InputStream content = null;
		String lRetVal = "";
		String lURL = "http://" + Config.IORDER_SERVER + Config.URL_ORDERED_MENU_ITEMS + "1";

		if (!isNetworkUp()) 
		{
			lRetVal = "Error: No network";
		} 
		else 
		{
			try 
			{
				HttpClient lClient = new DefaultHttpClient();
				HttpDelete lPostDel = new HttpDelete(lURL);
				
				HttpResponse lResponse = lClient.execute(lPostDel);
				content = lResponse.getEntity().getContent();
				lRetVal = getStringFromInputStream(content);
			} 
			catch (Exception lEx) 
			{
				lRetVal = "Error(0):" + lEx.getMessage();
			}
		}

		return lRetVal;
		
	}
	
	/*
	 * Convert InputStream to String
	 */
	private String getStringFromInputStream(InputStream is) 
	{
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		String line;

		try {
			br = new BufferedReader(new InputStreamReader(is));

			while ((line = br.readLine()) != null)
				sb.append(line);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return sb.toString();
	}

	
	/*
	 *
	 *
	 */
	public boolean isNetworkUp() 
	{
		ConnectivityManager connMgr = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

		if (networkInfo != null && networkInfo.isConnected())
			return (true);
		else
			return (false);
	}
}
